package com.ggb.reactive.repository

import com.ggb.reactive.domain.entity.Category
import org.springframework.data.r2dbc.repository.Modifying
import org.springframework.data.r2dbc.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.time.Instant

@Repository
interface CategoryRepository : ReactiveCrudRepository<Category, Long> {

    @Query("SELECT d FROM Category d WHERE (:name IS NULL OR d.name LIKE %:name%) AND (:type IS NULL OR d.type = :type)")
    fun findCategoriesByOptionalCriteria(
        @Param("name") name: String?,
        @Param("type") type: Int?,
    ): Flux<Category>

    // 根据类型查询分类
    fun findAllByType(type: Int): Flux<Category>

    @Modifying
    @Query("UPDATE category SET status = :status,update_time = :updateTime,update_user = :updateUser  WHERE id = :id")
    fun updateStatusById(id: Long, status: Int, updateTime: Instant, updateUser: Long): Mono<Int>
}